/*
******************************************************************************************************* 
**  Copyright (C) 2019, 苏州检易生物科技有限公司 
**  All rights reserved. 
** 
**  FileName:       	aux_task.c
**  Description:	
**  Author:        	 
**  Version				v0.1
**  Date:           	2019-05-09
**	Function List： 
**  History:         
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	调试开关
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                             INCLUDE FILES
*********************************************************************************************************
*/
#include "log_console.h"
#include "modbus.h"
#include "os_config.h"
#include <rtthread.h>
#include "timer.h"
#include "dmr9045.h"
/*
*********************************************************************************************************
*                                              	宏定义
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	结构体定义
*********************************************************************************************************
*/

 
/*
*********************************************************************************************************
*                                              	函数原型声明
*********************************************************************************************************
*/



/*
*********************************************************************************************************
*                                              	全局变量定义
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                              	模块静态变量定义
*********************************************************************************************************
*/



/*
*********************************************************************************************************
*                                              	函数定义
*********************************************************************************************************
*/




/*
*********************************************************************************************************
*                                               本地函数声明
*********************************************************************************************************
*/

static void test_tmr_callback(void* arg);
static void modbus_uart_recv_callback(void* arg,uint8_t *rxbuff,uint8_t rxsize);
/*
*********************************************************************************************************
*@brief:						
*********************************************************************************************************
*/
static struct MODBUS_T h_modbus;
static volatile uint8_t run_status = 0;
static volatile uint8_t modbus_ack;
static rt_timer_t	p_timer;
static __IO uint64_t tick = 0;
/*
*********************************************************************************************************
*
*********************************************************************************************************
*/
void dmr9045_start(void)
{
	if(MODBUS_ERRNONE != modbus_init(&h_modbus,UART7/*MOONS_SERVO_UART*/,115200))
	{
		Critical_Log("servo modbus failed!\r\n");
		return;
	}
	h_modbus.pfn_recv_callback = modbus_uart_recv_callback;
	p_timer = rt_timer_create("test timer",test_tmr_callback,RT_NULL,10,RT_TIMER_FLAG_PERIODIC);
	if(p_timer == NULL)
	{
		Critical_Log("%s create timer failed!\r\n","test timer");
	}
	else
		rt_timer_start(p_timer);
	run_status = 0;
	//电流设置
	uint16_t current = 300;
	modbus_write(&h_modbus,0x01,0x06,0x000D,1,&current);
	modbus_ack = 1;
	rt_thread_delay(200);
	uint16_t speed = 1000;
	modbus_write(&h_modbus,0x01,0x06,0x009A,1,&speed);
	modbus_ack = 1;
	//加速时间
	rt_thread_delay(200);
	uint16_t acc_time = 40;
	modbus_write(&h_modbus,0x01,0x06,0x0098,1,&acc_time);
	modbus_ack = 1;
	//减速时间
	rt_thread_delay(200);
	uint16_t dec_time = 40;
	modbus_write(&h_modbus,0x01,0x06,0x0099,1,&dec_time);
	modbus_ack = 1;
	//起步速度
	rt_thread_delay(200);
	uint16_t start_speed = 150;
	modbus_write(&h_modbus,0x01,0x06,0x0096,1,&start_speed);
	modbus_ack = 1;
	//起步速度
	rt_thread_delay(200);
	uint16_t stop_speed = 150;
	modbus_write(&h_modbus,0x01,0x06,0x0097,1,&stop_speed);
	modbus_ack = 1;
}
static void test_tmr_callback(void* arg)
{
	if(TIM_1K.cnt < 5000)
		return;
	if(modbus_ack)
		return;
	if(run_status)
	{
		//发送轮询
		modbus_ack = 1;
		modbus_read(&h_modbus,0x01,0x03,0x0006,0x0001);
	}
	else if(TIM_1K.cnt - tick > 2000)
	{
		//发送步数
		uint32_t step = 2718;
		run_status = 1;
		tick = TIM_1K.cnt;
		modbus_ack = 1;
		Trace_Log("DMR9045 run step %d,time:%d;\r\n",step,TIM_1K.cnt);
		modbus_write(&h_modbus,0x01,0x10,0x00CE,2,(uint16_t*)&step);
	}
}
static void modbus_uart_recv_callback(void* arg,uint8_t *rxbuff,uint8_t rxsize)
{
	read_single_ack_t* p_recv = (read_single_ack_t*)rxbuff;
	if(p_recv->fn == 0x03)
	{
		uint16_t status = (p_recv->addr_msb<<8) + p_recv->addr_lsb;
		if((status & 0x1000) && run_status == 1)
		{
			run_status = 0;
			Trace_Log("DMR9045 run finished,cost %d ms;\r\n",TIM_1K.cnt - tick);
			tick = TIM_1K.cnt;
		}
	}
	if(modbus_ack && (p_recv->fn == 0x03 || p_recv->fn == 0x06 || p_recv->fn == 0x04
		|| p_recv->fn == 0x10) )
	{
		
		modbus_ack = 0;
	}
}
/*
*********************************************************************************************************
*					
*********************************************************************************************************
*/
